package com.beans;

import com.classe.Postes;
import com.utils.CoordinateConversion;
import com.dao.PostesDAO;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.tree.TreeNode;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
import org.primefaces.model.map.Polyline;

public class PostesBean implements Serializable {

    private MapModel advancedModel;
    private Marker marker;
    String cor, cor2;
    private TreeNode[] selectedNodes;
    private TreeNode root;

    public PostesBean() {

        advancedModel = new DefaultMapModel();

        ArrayList<Postes> lista = PostesDAO.listar("");

        CoordinateConversion cc = new CoordinateConversion();

        HashMap<String, Polyline> poligonais = new HashMap<String, Polyline>();

        for (int i = 0; i < lista.size(); i++) {


            Postes postes = lista.get(i);
            double coordx;
            double coordy;

            String convertida = cc.utm2LatLon("22 J " + postes.getCoordx() + " " + postes.getCoordy());
            String[] utm = convertida.split(";");
            coordx = Double.parseDouble(utm[0]);
            coordy = Double.parseDouble(utm[1]);

            LatLng coord1 = new LatLng(coordx, coordy);

            if ("POSTE".equals(postes.getTipo())) {
                advancedModel.addOverlay(new Marker(coord1, "DADOS: " + postes.getCaract(), null, "http://maps.google.com/mapfiles/kml/pal4/icon56.png"));//"http://labs.google.com/ridefinder/images/mm_20_yellow.png"));
            } else {

                if ("CONDOR".equals(postes.getTipo())) {
                    cor2 = "0000FF";
                    cor = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
                } else if ("PANAMBI".equals(postes.getTipo())) {
                    cor2 = "FF0000";
                    cor = "http://labs.google.com/ridefinder/images/mm_20_red.png";

                } else if ("COPREL".equals(postes.getTipo())) {
                    cor2 = "FFA500";
                    cor = "http://labs.google.com/ridefinder/images/mm_20_orange.png";
                }
                Polyline polyline = poligonais.get(cor2);
                if (polyline == null) {
                    polyline = new Polyline();
                    polyline.setStrokeWeight(3);
                    polyline.setStrokeOpacity(1);
                    polyline.setStrokeColor(cor2);
                    poligonais.put(cor2, polyline);
                }
                
                polyline.getPaths().add(coord1);

                //advancedModel.addOverlay(new Marker(coord1, "DADOS: " + postes.getCaract(), null, cor));
            }
       }
        
        for (Map.Entry<String, Polyline> entry : poligonais.entrySet()) {
            
            Polyline polyline = entry.getValue();
            
            advancedModel.addOverlay(polyline);
            
        }
        

    }
    
    public TreeNode getRoot() {  
        return root;  
    } 
    
     public TreeNode[] getSelectedNodes() {  
        return selectedNodes;  
    } 

    public MapModel getAdvancedModel() {
        return advancedModel;
    }

    public void onMarkerSelect(OverlaySelectEvent event) {
        marker = (Marker) event.getOverlay();
    }

    public Marker getMarker() {
        return marker;
    }
}
